package com.hilinos.base.user.entity.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hilinos.common.util.EncryptUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * @author hilinos
 * @since 2022-03-08
 */
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
@ApiModel("用户账户表")
@TableName("user_account")
public class UserAccountPoJo implements Cloneable, Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("用户账号")
    @TableField("account_id")
    private String accountId;
    @ApiModelProperty("平台ID")
    @TableField("appid")
    private String appid;
    @ApiModelProperty("账户类型;如：微信，小程序")
    @TableField("account_type")
    private String accountType;
    @ApiModelProperty("机构ID")
    @TableField("org_id")
    private String orgId;
    @ApiModelProperty("用户_id")
    @TableField("user_id")
    private String userId;
    @ApiModelProperty("用户认证字符串")
    @TableField("account_secret")
    private String accountSecret;
    @ApiModelProperty("用户认证字符串额外参数")
    @TableField("account_secret_extra")
    private String accountSecretExtra;
    @ApiModelProperty("账户状态")
    @TableField("account_status")
    private Integer accountStatus;
    @ApiModelProperty("账户过期时间;为空表示永不过期，过期后，账户状态变更为停用")
    @TableField("account_exp_time")
    private String accountExpTime;
    @ApiModelProperty("创建人")
    private Long creator;
    @ApiModelProperty("创建人类型")
    @TableField("creator_type")
    private String creatorType;
    @ApiModelProperty("创建时间")
    @TableField("creator_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date creatorTime;
    @ApiModelProperty("更新人")
    private Long updator;
    @ApiModelProperty("更新人类型")
    @TableField("updator_type")
    private String updatorType;
    @ApiModelProperty("更新时间")
    @TableField("updator_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updatorTime;

    /**
     * 明文密码和盐生成数据库存放的密码
     *
     * @param password
     * @param salt
     * @return
     */
    @JsonIgnore
    public String genPasswordByClearText(String password, String salt) {
        return genPasswordByMd5(EncryptUtils.getMd5(password), salt);
    }

    /**
     * md5后的密码和盐生成数据库存放的密码
     *
     * @param password
     * @param salt
     * @return
     */
    @JsonIgnore
    public String genPasswordByMd5(String password, String salt) {
        return EncryptUtils.getMd5(password + salt);
    }

    /**
     * 明文密码和盐验证数据库存放的密码
     *
     * @param password
     * @return
     */
    @JsonIgnore
    public boolean checkPasswordByClearText(String password, String salt) {
        return genPasswordByClearText(password, salt).equals(accountSecret);
    }

    /**
     * md5后的密码和盐验证数据库存放的密码
     *
     * @param password
     * @return
     */
    @JsonIgnore
    public boolean checkPasswordByMD5(String password, String salt) {
        return genPasswordByMd5(password, salt).equals(accountSecret);
    }

}